package com.recyclerview422zfj.model;

import com.recyclerview422zfj.bean.VideoBean;
import com.recyclerview422zfj.iface.VideoListener;
import com.recyclerview422zfj.iface.Videoiface;
import com.recyclerview422zfj.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zengfanjun on 2017/6/8.
 */

public class VideoModel implements Videoiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    public VideoModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getResultList(String mod, int page, String sessionId, final VideoListener listener) {
        VideoService videoService=retrofit.create((VideoService.class));
        Call<List<VideoBean>> call=videoService.getVideoList(mod,page,sessionId);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    listener.onResponse(response.body());
                    //listener.onResponse(response.body());
                }
                else
                {
                    listener.onFail("FAIL");
                    //listener.onFaile("FAIL");
                }

            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
               listener.onFail(t.toString());
                //listener.onFaile(t.toString());

            }
        });

    }
}
